<link rel="stylesheet" href="/static/css/system/systemResourceTree.css" type="text/css">
<div class="resource-center">
    <div class="edit-button">
        <button class="layui-btn" onclick="saveResource()">确定</button>
        <button class="layui-btn layui-btn-primary" onclick="goback()">返回</button>
    </div>
    <ul id="resource" class="ztree"></ul>
</div>


<input type="hidden" id="hiddent_roleId" value="${roleId}">
<script>
    $.ajax({
        type: "POST",
        url: "/systemResource/getRoleResource?roleId=" + $('#hiddent_roleId').val(),
        success: function (data) {
            if (data != null) {
                // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
                var setting = {
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {"Y": "ps", "N": "ps"}
                    }
                };
                // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
                var zTreeObj = $.fn.zTree.init($("#resource"), setting, data);
            }
        }
    });

    function goback() {
        forwardByUrlAndTitle("/systemRole/index", "角色管理");
    }

    function saveResource() {
        var treeObj = $.fn.zTree.getZTreeObj("resource");
        var nodes = treeObj.getCheckedNodes();
        if (nodes.length > 0) {
            var resourceIds = "";
            for (var i = 0; i < nodes.length; i++) {
                resourceIds += nodes[i].id + ",";
            }

            //保存新权限
            $.ajax({
                type: "POST",
                url: "/systemResource/saveRoleResources",
                data: {"resourceIds": resourceIds, "roleId": $('#hiddent_roleId').val()},
                success: function (data) {
                    if (data != null && data.code == 200) {
                        layer.msg(data.msg);
                        setTimeout(function () {
                            forwardByUrlAndTitle("/systemRole/index", "角色管理");
                        }, 1000)
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        }else{
            layer.msg("请选择权限!");
        }

    }
</script>
